package com.example.mybatisplusdemo.web.controller;

import com.example.mybatisplusdemo.mapper.UserMapper;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.model.dto.UserDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IUserService;
import com.example.mybatisplusdemo.model.domain.User;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-17
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private IUserService userService;





    /**
    * 描述：根据Id 查询
    *
    */
    //http://127.0.0.1:8080/api/user/1
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<User> getById(@PathVariable("id") Long id)throws Exception {
        User user = userService.getById(id);
        return JsonResponse.success(user);
    }

    //接受请求
    @GetMapping("getdemo1")
    public String s(Long id,String name){
        System.out.println("id="+id);
        System.out.println("name="+name);
        return "hello";
    }

    //创建dto对象作为前后端交互的对象
    @GetMapping("getdemo2")
    public String getdemo2(UserDTO userDTO){
        System.out.println("id="+userDTO.getId());
        System.out.println("name="+userDTO.getName());
        return "你好";
    }

    //占位符传参
    @GetMapping("getdemo3/{id}/{name}")
    public String getdemo3(@RequestParam("id") Long id, @RequestParam("name") String name) {
        System.out.println("id=" + id);
        System.out.println("name=" + name);
        return "你好";
    }

    //post请求
    //json请求——以对象形式传递数据
    @PostMapping("postdemo1")
    public Map postdemo1(@RequestBody User user){
        System.out.println("id=" + user.getLoginName());

        Map<String, Object> map = new HashMap<>();
        map.put("data","baibai");
        return map;
    }

    @PostMapping("post")
    public Map post(@RequestBody User user){
        User newuser = userService.getById(user.getId());
        System.out.println("id=" + newuser.getLoginName());
        Map<String,Object> map = new HashMap<>();
        map.put("data",newuser.getLoginName());
        return map;
    }

    @PostMapping("postdemo2")
    public Map postdemo2(@RequestBody PageDTO page){

    }
}

